<?php 
class Webservices_Comments {
    private $_modelComments;
    private $_modelUsers;
    
    public function __construct(){
        $this->_modelComments = new Application_Model_Comments();
        $this->_modelUsers = new Application_Model_Users();
    }
    
    /**
     * Remove comment
     * @param string $username
     * @param string $password
     * @param string $commentId
     * @return int
     */
    public function removeComment($username, $password, $commentId){
        $res = 0;
        if($this->_modelUsers->login($username, $password)){
            $res = $this->_modelComments->delete("comment_id = ".$commentId);
        }
        return $res;
    }
    
    /**
     * Get comment
     * @param string $username
     * @param string $password
     * @return string
     */
    public function getCommentsByUsername($username, $password){
       $res = array();
       if($this->_modelUsers->login($username, $password)){
            $comments = $this->_modelComments->getCommentsByUsername($username);
            if($comments){
                $res = $comments->toArray();
                foreach($res as $k => $item){
                    $this->_modelComments->convertField($res[$k]);
                }
            }
       }
       return json_encode($res);
       
    }
    
    /**
     * Get comment
     * @param string $username
     * @param string $password
     * @param string $commentId
     * @return string
     */
    public function getComment($username, $password, $commentId){
        $res = array();
        $comment = $this->_modelComments->find($commentId)->current();
        if($comment){
            $res = $comment->toArray();
            $this->_modelComments->convertField($res);
        }
        return json_encode($res);
    }
    
    /**
     * Get user comment by event id
     * @param string $username
     * @param string $password
     * @param string $eventId
     * @return string
     */
    public function getCommentsByEventId($username, $password, $eventId){
        $res = array();
        $comments = $this->_modelComments->fetchAll("event_id = ".$eventId);
        if($comments){
            $res = $comments->toArray();
            foreach($res as $key => $item){
                $this->_modelComments->convertField($res[$key]);
            }
        }
        return json_encode($res);    
    }
    
    /**
     * Get user comment by user id
     * @param string $username
     * @param string $password
     * @param string $userId
     * @return string
     */
    public function getCommentsByUserId($username, $password, $userId){
        $res = array();
        $comments = $this->_modelComments->fetchAll("user_id = ".$userId);
        if($comments){
            $res = $comments->toArray();
            foreach($res as $key => $item){
                $this->_modelComments->convertField($res[$key]);
            }
        }
        return json_encode($res);
    }
    
    /**
     * Update data
     * @param string $username
     * @param string $password
     * @param string $data
     * @return boolean
     */
    public function updateComment($username, $password, $data){
        $res = false; 
        if($this->_modelUsers->login($username, $password)){
            $obj = json_decode($data);
            $data = $this->_modelComments->convertObjectToArray($obj);
            $this->_modelComments->convertField2($data);
            
            //add comment
            $res = $this->_modelComments->edit($data);
        } 
        return $res;
    }
    
    /**
     * Add comment
     * @param string $usename
     * @param string $password
     * @param string $data
     * @return int
     */
    public function addComment($username, $password, $data){
        $res = 0; 
        if($this->_modelUsers->login($username, $password)){
            $obj = json_decode($data);
            $data = $this->_modelComments->convertObjectToArray($obj);
            $this->_modelComments->convertField2($data);
            
            //add comment
            unset($data['comment_id']);
            $res = $this->_modelComments->add($data);
        } 
        return $res;
    }
    
}